package com.ctc.kmpdemo.net

import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.defaultRequest
import io.ktor.http.URLProtocol
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json

/**
 * 网络请求封装
 */
object NetworkClient {
    // 创建HttpClient
    val client: HttpClient = HttpClient(createHttpClientEngine()) {
        defaultRequest {
            host = "www.wanandroid.com"
            url {
                protocol = URLProtocol.HTTPS
            }
        }
        install(ContentNegotiation) {
            json(Json {
                prettyPrint = true
                isLenient = true
                ignoreUnknownKeys = true
            })
        }
    }
}

/**
 * 创建HttpClientEngine
 * 根据平台创建不同的HttpClientEngine
 */
expect fun createHttpClientEngine(): HttpClientEngine